﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenQuarters.WebQuarters.Core;

namespace OpenQuarters.WebQuarters.Modules.Flash
{
    public class FlashModule : Module
    {

        private string swfPath = "/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Modules.Flash&filename=Resources.flash_flashnotfound.swf";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.Flash)]
        public string Swf_Path
        {
            get { return swfPath; }
            set { swfPath = value; }
        }
        
        private string alternativeImagePath = "/CMS/File/Resource?Assembly=OpenQuarters.WebQuarters.Core.Web&filename=Areas.CMS.Content.Images.image_imagenotfound.gif";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.Image)]
        public string AlternativeImage
        {
            get { return alternativeImagePath; }
            set { alternativeImagePath = value; }
        }

        private string width = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Width
        {
            get { return width; }
            set { width = value; }
        }

        private string height = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string Height
        {
            get { return height; }
            set { height = value; }
        }

        private string wmode = "transparent";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string WMode
        {
            get { return wmode; }
            set { wmode = value; }
        }

        private string flashvars = "";

        [Newtonsoft.Json.JsonProperty]
        [PropertyConfiguration(PropertyType.TextBox)]
        public string FlashVars
        {
            get { return flashvars; }
            set { flashvars = value; }
        }

        public override void Initialise()
        {
            this.ModuleName = "Flash Module";
            this.ModuleDescription = "Places a Flash animation on the page.";
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            base.Controls.Add(new LiteralControl("<div id=\"" + this.ModuleId + "\">"));
            if (string.IsNullOrEmpty(alternativeImagePath))
            {
                Image image = new Image();
                image.ImageUrl = alternativeImagePath;
                base.Controls.Add(image);
            }
            base.Controls.Add(new LiteralControl("</div>"));

            JavaScript.RegisterScript("jQuery.Flash.js", "OpenQuarters.WebQuarters.Modules.Flash", "Resources.jQuery.Flash.js", JavaScriptType.PageScript);

            StringBuilder startupScript = new StringBuilder();
            startupScript.Append(@"
                $('#" + this.ModuleId + @"').flash({
                    src: '" + swfPath + @"'");

            if (!string.IsNullOrEmpty(width))
                startupScript.AppendLine(", width: " + width + "");

            if (!string.IsNullOrEmpty(height))
                startupScript.AppendLine(", height: " + height + "");

            if (!string.IsNullOrEmpty(wmode))
                startupScript.AppendLine(", wmode: '" + wmode + "'");

            if (!string.IsNullOrEmpty(flashvars))
                startupScript.AppendLine(", flashvars: {" + flashvars + "}");

            startupScript.Append("});");
            JavaScript.RegisterScript("Modules.Flash.StartupScript_" + this.ModuleId + ".js", startupScript.ToString(), JavaScriptType.ModuleStartupScript);
        }
    }
}
